package com.eyaomall.wxmanage.controller;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.eyaomall.wxmanage.common.helper.OssHelper;
import com.eyaomall.wxmanage.model.pojo.ResFlag;
import com.eyaomall.wxmanage.model.pojo.ResOss;
import com.lycheeframework.core.cmp.api.http.GsnData;
import com.lycheeframework.core.cmp.api.http.GsnResponse;
import com.lycheeframework.core.common.helper.GlobEnv;
import com.lycheeframework.core.common.util.encrypt.SHA256;

@Controller
@RequestMapping("file")
public class FileController {

	/**
	 * 文件上传
	 * @param key - 文件夹名
	 * @param request 
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("upload")
	public @ResponseBody GsnData upload(String key, HttpServletRequest request) throws IOException{
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultipartFile multipartFile = multipartRequest.getFile("Filedata");
		return OssHelper.upload(key,multipartFile);
//		ResOss ro =  new ResOss();
//		ro.setKey(key);
//		ro.setFilePath(QiniuHelper.upload(multipartFile));
//		return ApiResponse.toApiData(ro);
//		return ImageHelper.upload(key, multipartFile);
	}
	
	/**
	 * 文件上传-本地
	 * @param key - 文件夹名
	 * @param request 
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("upload_file")
	public @ResponseBody GsnData uploadFile(String key, HttpServletRequest request) throws IOException{
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultipartFile excelFile = multipartRequest.getFile("Filedata");
		String filename = "";
		if (excelFile != null) {
//			System.out.println(excelFile.getName()+"--"+excelFile.getSize());
			filename = SHA256.encrypt(String.valueOf(new Date().getTime())).substring(0, 24) + "." + FilenameUtils.getExtension(excelFile.getOriginalFilename());
			SaveFileFromInputStream(excelFile.getInputStream(),GlobEnv.get("excel.tmp"),filename);//保存到服务器的路径
		}
		ResOss ro =  new ResOss();
		ro.setKey(key);
		ro.setFilePath(filename);
		return GsnResponse.toData(ro);
	}
	
	/**
	 * 保存文件
	 * @param stream
	 * @param path
	 * @param savefile
	 * @throws IOException
	 */
	private void SaveFileFromInputStream(InputStream stream,String path,String savefile) throws IOException
	{
       FileOutputStream fs = new FileOutputStream( path + "/"+ savefile);
       byte[] buffer = new byte[1024*1024];
       int byteread = 0; 
       while ((byteread=stream.read(buffer))!=-1)
       {
          fs.write(buffer,0,byteread);
          fs.flush();
       } 
       fs.close();
       stream.close();   
	}
	
	/**
	 * 文件上传
	 * @param key - 文件夹名
	 * @param request 
	 * @return
	 */
	@RequestMapping("delete")
	public @ResponseBody GsnData delete(String path){
		
		return GsnResponse.toData(new ResFlag(OssHelper.delObject(path)));
	}
	
	/**
	 * 编辑器上传图片
	 * @throws IOException 
	 * @reutrn string
	 */
	@RequestMapping("edit_upload_pic")
	public @ResponseBody
	GsnData uploadEditPic(HttpServletRequest request,String key) {

		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultipartFile multipartFile = multipartRequest.getFile("pic");
		
		return OssHelper.upload(key,multipartFile);
	}
}
 